package com.wwwarehouse.usercenter.fragment.permissiondistribute;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.fastjson.JSON;
import com.wwwarehouse.common.activity.base.BaseFragment;
import com.wwwarehouse.common.constant.Constant;
import com.wwwarehouse.common.custom_widget.CircleImageView;
import com.wwwarehouse.common.tools.ImageloaderUtils;
import com.wwwarehouse.usercenter.R;
import com.wwwarehouse.usercenter.bean.authoritydistribute.AuthNotificationBean;
import com.wwwarehouse.usercenter.constant.UserCenterConstant;


/**
 * @author : mundane
 * @time : 2017/9/27 22:28
 * @description :
 * @file : AuthNotificationFragment.java
 */
@Route(path = UserCenterConstant.PATH_AUTHORITY_NOTIFICATION)
public class AuthNotificationFragment extends BaseFragment {


	private AuthNotificationBean mAuthNotificationBean;
	private CircleImageView mIvAvatar;
	private TextView mTv;
	private Button mBtnAllAuthority;
	private Button mBtnNotSuccessAuthority;
	private TextView mTvAuthorityResult;
	private View mBtnIKnow;

	public static AuthNotificationFragment newInstance() {
		AuthNotificationFragment fragment = new AuthNotificationFragment();
		Bundle args = new Bundle();
		fragment.setArguments(args);
		return fragment;
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Bundle bundle = getArguments();
		if (bundle != null) {
			String msg = bundle.getString(Constant.KEY_AUTHORITY_NOTIFICATION);
			mAuthNotificationBean = JSON.parseObject(msg, AuthNotificationBean.class);
		}
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.fragment_notification_auth, container, false);
		return view;
	}

	@Override
	public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
		// findViewById
		mIvAvatar = $(R.id.iv_avatar);
		mTv = $(R.id.tv);
		mBtnIKnow = $(R.id.btn_i_know);
		mBtnAllAuthority = $(R.id.btn_all_authority);
		mBtnNotSuccessAuthority = $(R.id.btn_not_success_authority);
		mTvAuthorityResult = $(R.id.tv_authority_result);
		mBtnIKnow.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				popFragment();
			}
		});
		if (mAuthNotificationBean != null) {
			ImageloaderUtils.displayImg(mAuthNotificationBean.msg.faceUrl, mIvAvatar);
			String userName = mAuthNotificationBean.msg.auUserName;
			String businessName = mAuthNotificationBean.msg.businessName;
			final String processId = mAuthNotificationBean.msg.processId;
			SpannableString spannableString = new SpannableString(String.format("%s %s 要分配权限给你", businessName, userName));
			ForegroundColorSpan colorSpan = new ForegroundColorSpan(getResources().getColor(R.color.common_color_c1_337cff));
			spannableString.setSpan(colorSpan, businessName.length() + 1, businessName.length() + 1 + userName.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
			mTv.setText(spannableString);

			int status = mAuthNotificationBean.msg.status;
			switch (status) {
				case 0:
					mBtnNotSuccessAuthority.setVisibility(View.GONE);
					mTvAuthorityResult.setText(String.format("共%d个权限, 所有权限均分配成功", mAuthNotificationBean.msg.authTotal));
					break;
				case 2:
					mTvAuthorityResult.setText(String.format("共%d个权限, 部分权限未分配成功", mAuthNotificationBean.msg.authTotal));
					break;
			}
			mBtnAllAuthority.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					pushFragment(AuthorityDistributeAllAuthDetailPageFragment.newInstance(processId));
				}
			});
			mBtnNotSuccessAuthority.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {

					pushFragment(AuthorityDistributeFailAuthDetailPageFragment.newInstance(processId, 1));
				}
			});
		}

	}

	@Override
	public void setTitle() {
		if (peekFragment() instanceof AuthNotificationFragment) {
			mActivity.setTitle("权限分配");
		}
	}

	@Override
	public void requestDatas() {

	}
}
